home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / GNUPLOTsrc.lha / term / pstex.trm < prev    next >
Encoding:
Text File  |  1996-01-22  |  10.0 KB  |  392 lines

  1. /*
  2.  * $Id: pstex.trm,v 1.2 1995/05/25 14:31:47 drd Exp $
  3.  */
  4.  
  5. /* GNUPLOT - pstex.trm */
  6. /*
  7.  * Copyright (C) 1990 - 1993   
  8.  *
  9.  * Permission to use, copy, and distribute this software and its
  10.  * documentation for any purpose with or without fee is hereby granted, 
  11.  * provided that the above copyright notice appear in all copies and 
  12.  * that both that copyright notice and this permission notice appear 
  13.  * in supporting documentation.
  14.  *
  15.  * Permission to modify the software is granted, but not the right to
  16.  * distribute the modified code.  Modifications are to be distributed 
  17.  * as patches to released version.
  18.  *  
  19.  * This software  is provided "as is" without express or implied warranty.
  20.  * 
  21.  * This file is included by ../term.c.
  22.  *
  23.  * This terminal driver supports:
  24.  *     Generic TeX with embedded PostScript
  25.  *
  26.  * AUTHORS
  27.  *  George Phillips
  28.  *  Russell Lang
  29.  *  David Kotz
  30.  * 
  31.  * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
  32.  */
  33.  
  34. /* Driver by Pieter Vosbeek from the PSLaTeX driver by George Phillips */
  35.  
  36. void PSTEX_options __P((void));
  37. void PSTEX_PS_init __P((void));
  38. void PSTEX_init __P((void));
  39. int PSTEX_scale __P((double xs, double ys));
  40. void PSTEX_PS_graphics __P((void));
  41. void PSTEX_graphics __P((void));
  42. void PSTEX_put_text __P((unsigned int x, unsigned int y, char str[]));
  43. int PSTEX_justify_text __P((enum JUSTIFY mode));
  44. int PSTEX_text_angle __P((int angle));
  45. void PSTEX_reset __P((void));
  46. void PSTEX_text __P((void));
  47.  
  48. #define PSTEX_XMAX (5*720)
  49. #define PSTEX_YMAX (3*720)
  50.  
  51. /* 10 pt char is about 10 pts high (say) */
  52. #define PSTEX_VCHAR (100)
  53. /* 10 pt char is about 6 pts wide (say) */
  54. #define PSTEX_HCHAR (60)
  55.  
  56. static int PSTEX_angle;
  57. static int PSTEX_justify;
  58. static int PSTEX_rotate = TRUE;
  59.  
  60. static struct text_command* PSTEX_labels;
  61.  
  62. #define PSTEX_PS_XOFF    50    /* page offset in pts */
  63. #define PSTEX_PS_YOFF    50
  64.  
  65. #define PSTEX_PS_XMAX 7200
  66. #define PSTEX_PS_YMAX 5040
  67.  
  68. #define PSTEX_PS_VTIC (PSTEX_PS_YMAX/120)
  69. #define PSTEX_PS_HTIC (PSTEX_PS_YMAX/120)
  70.  
  71. #define PSTEX_PS_SC (10)        /* scale is 1pt = 10 units */
  72. #define    PSTEX_PS_LW (0.5)        /* default linewidth = 0.5 pts */
  73.  
  74. static double PSTEX_linewidth = PSTEX_PS_LW;
  75.  
  76. void PSTEX_options()
  77. {
  78.   extern double real();
  79.   extern struct value *const_express();
  80.   int fontsize = 10;
  81.   struct value a;
  82.  
  83.   if (!END_OF_COMMAND) {
  84.     if (almost_equals(c_token, "d$efault")) {
  85.       ps_color = FALSE;
  86.       PSTEX_rotate = TRUE;
  87.       PSTEX_linewidth = PSTEX_PS_LW;
  88.       term->v_char = (unsigned int)PSTEX_VCHAR;
  89.       term->h_char = (unsigned int)PSTEX_HCHAR;
  90.       c_token++;
  91.     }
  92.   }
  93.   
  94.   if (!END_OF_COMMAND)
  95.     {
  96.       if (almost_equals(c_token, "c$olor"))
  97.     {
  98.       ps_color = TRUE;
  99.       c_token++;
  100.     }
  101.       else if (almost_equals(c_token, "m$onochrome"))
  102.     {
  103.       ps_color = FALSE;
  104.       c_token++;
  105.     }
  106.     }
  107.   
  108.   if (!END_OF_COMMAND)
  109.     {
  110.       if (almost_equals(c_token, "r$otate"))
  111.     {
  112.       PSTEX_rotate = TRUE;
  113.       c_token++;
  114.     }
  115.       else if (almost_equals(c_token, "n$orotate"))
  116.     {
  117.       PSTEX_rotate = FALSE;
  118.       c_token++;
  119.     }
  120.     }
  121.   
  122.   
  123.   if (!END_OF_COMMAND)
  124.     {
  125.       if (almost_equals(c_token, "f$ontsize"))
  126.     {
  127.       c_token++;
  128.       if (!END_OF_COMMAND)
  129.         {
  130.           fontsize = (int)real(const_express(&a));
  131.           term->v_char = (unsigned int)(fontsize*PSTEX_PS_SC);
  132.           term->h_char = (unsigned int)(fontsize*PSTEX_PS_SC*6/10);
  133.         }
  134.     }
  135.     }
  136.   
  137.   if (!END_OF_COMMAND)
  138.     {
  139.       if (almost_equals(c_token, "l$inewidth"))
  140.     {
  141.       c_token++;
  142.       if (!END_OF_COMMAND)
  143.         PSTEX_linewidth = real(const_express(&a));
  144.     }
  145.     }
  146.   
  147.   sprintf(term_options, "%s %s fontsize %d linewidth %g",
  148.       ps_color ? "color" : "monochrome",
  149.       PSTEX_rotate ? "rotate" : "norotate",
  150.       fontsize, PSTEX_linewidth);
  151. }
  152.  
  153. void PSTEX_PS_init()
  154. {
  155.   static char GPFAR psi1[] = "%%%%Creator: gnuplot\n\
  156. %%%%DocumentFonts: %s\n\
  157. %%%%BoundingBox: %d %d ";
  158.   static char GPFAR psi2[] = "%%%%EndComments\n\
  159. /gnudict 40 dict def\ngnudict begin\n\
  160. /Color %s def\n\
  161. /Solid %s def\n\
  162. /gnulinewidth %.3f def\n\
  163. /vshift %d def\n\
  164. /dl {%d mul} def\n\
  165. /hpt %.1f def\n\
  166. /vpt %.1f def\n";
  167.  
  168.   struct termentry *t = term;
  169.   int i;
  170.   ps_page = 0;
  171.  
  172.   if (!ps_eps)
  173.     fprintf(outfile,"%%!PS-Adobe-2.0\n");
  174.   else
  175.     fprintf(outfile,"%%!PS-Adobe-2.0 EPSF-2.0\n");
  176.  
  177.   fprintf(outfile, psi1, ps_font, PSTEX_PS_XOFF, PSTEX_PS_YOFF);
  178.   if (ps_portrait)
  179.     fprintf(outfile,"%d %d\n", 
  180.         (int)(xsize*(ps_eps ? 0.5 : 1.0)
  181.           *(PSTEX_PS_XMAX)/PSTEX_PS_SC+0.5+PSTEX_PS_XOFF), 
  182.         (int)(ysize*(ps_eps ? 0.5 : 1.0)
  183.           *(PSTEX_PS_YMAX)/PSTEX_PS_SC+0.5+PSTEX_PS_YOFF));
  184.   else 
  185.     fprintf(outfile,"%d %d\n", 
  186.         (int)(ysize*(ps_eps ? 0.5 : 1.0)
  187.           *(PSTEX_PS_YMAX)/PSTEX_PS_SC+0.5+PSTEX_PS_XOFF), 
  188.         (int)(xsize*(ps_eps ? 0.5 : 1.0)
  189.           *(PSTEX_PS_XMAX)/PSTEX_PS_SC+0.5+PSTEX_PS_YOFF));
  190.   if (!ps_eps)
  191.     fprintf(outfile,"%%%%Pages: (atend)\n");
  192.   fprintf(outfile, psi2,
  193.       ps_color ? "true" : "false",
  194.       ps_solid ? "true" : "false",
  195.       PSTEX_linewidth*PSTEX_PS_SC,    /* line width */
  196.       (int)(t->v_char)/(-3),    /* shift for vertical centring */
  197.       PSTEX_PS_SC,            /* dash length */
  198.       PSTEX_PS_HTIC/2.0,        /* half point width */
  199.       PSTEX_PS_VTIC/2.0);        /* half point height */
  200.   
  201.   for ( i=0; PS_header[i] != NULL; i++)
  202.     fprintf(outfile,"%s",PS_header[i]);
  203.   fprintf(outfile,"end\n%%%%EndProlog\n");
  204. }
  205.  
  206. void PSTEX_init()
  207. {
  208.   /* reset PostScript driver variables */
  209.   ps_portrait = TRUE;
  210.   ps_eps = FALSE;
  211.   ps_color = FALSE;
  212.  
  213.   fprintf(outfile, "%% GNUPLOT: TeX with Postscript \\specials\n");
  214.   fprintf(outfile, "\\expandafter\\ifx\\csname PSTeXpicture\\endcsname\\relax\n");
  215.   fprintf(outfile, "\\newdimen\\PSTeXunit \\PSTeXunit=0.1bp\n");
  216.   fprintf(outfile, "\\def\\PSTeXpicture(#1,#2){");
  217.   fprintf(outfile, "\\vbox to#2\\PSTeXunit\\bgroup\n");
  218.   fprintf(outfile, "  \\def\\put(##1,##2)##3{\\unskip\\raise##2\\PSTeXunit\n");
  219.   fprintf(outfile, "    \\rlap{\\kern##1\\PSTeXunit ##3}\\ignorespaces}%%\n");
  220.   fprintf(outfile, "  \\def\\lrlap##1{\\hbox to0pt{\\hss ##1\\hss}}%%\n");
  221.   fprintf(outfile, "  \\def\\ulap##1{\\vbox to0pt{\\vss ##1}}%%\n");
  222.   fprintf(outfile, "  \\def\\dlap##1{\\vbox to0pt{##1\\vss}}%%\n");
  223.   fprintf(outfile, "  \\def\\udlap##1{\\vbox to0pt{\\vss ##1\\vss}}%%\n");
  224.   fprintf(outfile, "  \\def\\stack##1{\\halign{\\hfil ####\\hfil\\cr ##1\\crcr}}%%\n");
  225.   fprintf(outfile, "  \\vss\\hbox to#1\\PSTeXunit\\bgroup\\ignorespaces}%%\n");
  226.   fprintf(outfile, "\\def\\endPSTeXpicture{\\hss\\egroup\\egroup}\\fi\n");
  227.   fprintf(outfile, "\\special{!\n");
  228.   PSTEX_PS_init();
  229.   fprintf(outfile, "}\n");
  230.   
  231.   PSTEX_angle = 0;
  232.   PSTEX_justify = 0;
  233.   PSTEX_labels = 0;
  234. }
  235.  
  236. int PSTEX_scale(xs, ys)
  237.      double xs, ys;
  238. {
  239.   register struct termentry *t = term;
  240.   
  241.   t->xmax = (unsigned int)(PSTEX_XMAX * xs);
  242.   t->ymax = (unsigned int)(PSTEX_YMAX * ys);
  243.   
  244.   return TRUE;
  245. }
  246.  
  247. void PSTEX_PS_graphics()
  248. {
  249.   static char GPFAR psg1[] = "0 setgray\n/%s findfont %d scalefont setfont\nnewpath\n";
  250.   struct termentry *t = term;
  251.  
  252.   ps_page++;
  253.   if (!ps_eps)
  254.     fprintf(outfile,"%%%%Page: %d %d\n",ps_page,ps_page);
  255.  
  256.   fprintf(outfile,"gnudict begin\ngsave\n");
  257.   fprintf(outfile,"%d %d translate\n",PSTEX_PS_XOFF,PSTEX_PS_YOFF);
  258.   fprintf(outfile,"%.3f %.3f scale\n", (ps_eps ? 0.5 : 1.0)/PSTEX_PS_SC,
  259.       (ps_eps ? 0.5 : 1.0)/PSTEX_PS_SC);
  260.   if (!ps_portrait)
  261.     {
  262.       fprintf(outfile, "90 rotate\n0 %d translate\n",
  263.           (int)(-PSTEX_PS_YMAX*ysize));
  264.     }
  265.   fprintf(outfile, psg1, ps_font, (t->v_char) );
  266.   ps_path_count = 0;
  267.   PS_relative_ok = FALSE;
  268.   PS_pen_x = PS_pen_y = -4000;
  269.   PS_taken = 0;
  270.   PS_linetype_last = -1;
  271. }
  272.  
  273. void PSTEX_graphics()
  274. {
  275.   struct termentry *t = term;
  276.  
  277. /*  
  278.   fprintf(outfile, "\\vbox to%d\\PSTeXunit{\\vss\n", t->ymax);
  279.   fprintf(outfile, "\\hbox to%d\\PSTeXunit{%%\n", t->xmax);
  280. */
  281.   fprintf(outfile, "\\PSTeXpicture(%d,%d)\n", t->xmax, t->ymax);
  282.   
  283.   fprintf(outfile, "\\special{\"\n");
  284.   PSTEX_PS_graphics();
  285.   /* thwart the translation done by PS_graphics() */
  286.   fprintf(outfile, "%f %f translate\n",
  287.       -PSTEX_PS_XOFF * (float)PSTEX_PS_SC, -PSTEX_PS_YOFF * (float)PSTEX_PS_SC);
  288.   PSTEX_labels = (struct text_command *)NULL;
  289. }
  290.  
  291. void PSTEX_put_text(x, y, str)
  292.      unsigned int x, y;
  293.      char str[];
  294. {
  295.   struct text_command* tc;
  296.   
  297.   /* ignore empty strings */
  298.   if (str[0] == '\0')
  299.     return;
  300.   
  301.   tc = (struct text_command*)alloc(sizeof(struct text_command),"pstex");
  302.   tc->x = x;
  303.   tc->y = y;
  304.   tc->label = (char *)alloc(strlen(str) + 1,"pstex");
  305.   strcpy(tc->label, str);
  306.   tc->justify = PSTEX_justify;
  307.   tc->angle = PSTEX_angle;
  308.   
  309.   tc->next = PSTEX_labels;
  310.   PSTEX_labels = tc;
  311. }
  312.  
  313. int PSTEX_justify_text(mode)
  314.      enum JUSTIFY mode;
  315. {
  316.   PSTEX_justify = mode;
  317.   return TRUE;
  318. }
  319.  
  320. int PSTEX_text_angle(angle)
  321.      int angle;
  322. {
  323.   /* rotated text is put in a short stack, and optionally uses 
  324.    * postscript specials depending on PSTEX_rotate */
  325.   PSTEX_angle = angle;
  326.   return TRUE;
  327. }
  328.  
  329.  
  330. void PSTEX_reset()
  331. {
  332. }
  333.  
  334. void PSTEX_text()
  335. {
  336.   struct text_command* tc;
  337.   
  338.   PS_text();
  339.   fprintf(outfile, "}\n");
  340.   
  341.   for (tc = PSTEX_labels; tc != (struct text_command*)NULL; tc = tc->next) {
  342.     fprintf(outfile, "\\put(%d,%d){", tc->x, tc->y);
  343.     switch (tc->angle) {
  344.     case 0:
  345.       switch (tc->justify) {
  346.       case LEFT:
  347.     fprintf(outfile, "\\udlap{\\rlap{%s}}", tc->label);
  348.     break;
  349.       case CENTRE:
  350.     fprintf(outfile, "\\udlap{\\lrlap{%s}}", tc->label);
  351.     break;
  352.       case RIGHT:
  353.     fprintf(outfile, "\\udlap{\\llap{%s}}", tc->label);
  354.     break;
  355.       }
  356.       break;
  357.     case 1: /* put text in a short stack */
  358.       if (PSTEX_rotate) {
  359.     fprintf(outfile, "%%\n\\special{ps: gsave currentpoint currentpoint translate\n");
  360.     fprintf(outfile, "270 rotate neg exch neg exch translate}%%\n");
  361.       }
  362.       switch (tc->justify) {
  363.       case LEFT:
  364.     fprintf(outfile, "\\rlap{\\ulap{\\stack{%s}}}", tc->label);
  365.     break;
  366.       case CENTRE:
  367.     fprintf(outfile, "\\lrlap{\\ulap{\\stack{%s}}}", tc->label);
  368.     break;
  369.       case RIGHT:
  370.     fprintf(outfile, "\\llap{\\dlap{\\stack{%s}}}", tc->label);
  371.     break;
  372.       }
  373.       if (PSTEX_rotate) {
  374.     fprintf(outfile, "%%\n\\special{ps: currentpoint grestore moveto}%%\n");
  375.       }
  376.     }
  377.     fprintf(outfile, "}%%\n");
  378.   }
  379.   
  380.   while (PSTEX_labels) {
  381.     tc = PSTEX_labels->next;
  382.     free(PSTEX_labels->label);
  383.     free(PSTEX_labels);
  384.     PSTEX_labels = tc;
  385.   }
  386. /*  
  387.   fprintf(outfile, "\\hss}}\n");
  388. */
  389.   fprintf(outfile, "\\endPSTeXpicture\n");
  390. }
  391.  
  392.